#include "functions.h"

void initialize_individuals(){
  int i, j;

  individuals = ( INDIVIDUAL * ) malloc( sizeof( INDIVIDUAL ) * population_size );

  for( i = 0; i < population_size; i++ ){
    individuals[ i ].design_variables = ( double * ) malloc( sizeof( double ) * number_of_dimensions );
  
    for( j = 0; j < number_of_dimensions; j++ ){
      individuals[ i ].design_variables[ j ] = get_random_double_value( lower_limit, upper_limit );
    }

    individuals[ i ].function_value = evaluate( individuals[ i ].design_variables );

  } 
  
}

void population_sort(){
  qsort( individuals, population_size, sizeof(INDIVIDUAL), (int(*)(const void*, const void*))individual_comp );
}

int individual_comp(const INDIVIDUAL *v0, const INDIVIDUAL *v1){

  if( strcmp( function_name, "f1") == 0 || strcmp( function_name, "f3") == 0 ){

    if(v0->function_value > v1->function_value)
      return -1;
    else
      return 1;

  } else{

    if(v0->function_value < v1->function_value)
      return -1;
    else
      return 1;
  }
}
